package admin.controller;

import admin.common.Result;
import admin.entity.User;
import admin.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 新增
     */
    @PostMapping
    public Result addItemInfo(@RequestBody User user) {
        userService.save(user);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        userService.removeById(id);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        userService.updateById(user);
        return Result.ok();
    }


    /**
     * 查询
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable String id) {
        User user = userService.getById(id);
        return Result.ok().data(user);
    }

    /**
     * 分页查询
     */
    @GetMapping
    public Result page(Page<User> page, User user) {
        userService.page(page, user);
        return Result.ok().data(page);

    }
}
